package com.house.keeper.service.impl;

import com.house.keeper.entity.CityArea;
import com.house.keeper.entity.CommunityInfo;
import com.house.keeper.entity.bean.CommunityInfoBean;
import com.house.keeper.entity.vo.CommunityInfoVo;
import com.house.keeper.mapper.CityAreaMapper;
import com.house.keeper.mapper.CommunityInfoMapper;
import com.house.keeper.service.CommunityInfoService;
import com.house.keeper.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.keeper.base.Result;
import com.house.keeper.base.ResultCode;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zjj
 * @date 2022-05-23 14:27:04
 *
 * @description 
 */
@Service
public class CommunityInfoServiceImpl implements CommunityInfoService {
    @Autowired
    private CommunityInfoMapper communityInfoMapper;
    @Autowired
    private CityAreaMapper cityAreaMapper;

    /**
     * 分页查询
     *
     * @param communityInfoVo 查询条件
     * @return
     */
    @Override
    public Result selectByPage(CommunityInfoVo communityInfoVo) {
        PageHelper.startPage(communityInfoVo.getPage(),communityInfoVo.getLimit());

        List<CommunityInfoBean> communityInfoList = communityInfoMapper.selectCommunityInfo(communityInfoVo);

        PageInfo<CommunityInfoBean> pageInfo = new PageInfo<>(communityInfoList);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Result delete(Integer id) {
        int row = communityInfoMapper.deleteById(id);
        if(row<=0){
            return Result.failure(ResultCode.PARAM_IS_INVALID);
        }
        return Result.success();
    }

    /**
     * 添加或修改
     *
     *  回滚：一般抛出的是RuntimeExcept异常
     *  在spring框架下，所有的异常都被org.springframework重写为RuntimeExcept
     * @param communityInfoBean
     * @return
     */
    @Override
    @Transactional
    public Result update(CommunityInfoBean communityInfoBean) {
//        根据省市查询数据
        QueryWrapper<CityArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_area_code",communityInfoBean.getParentAreaCode());
        queryWrapper.eq("area_code",communityInfoBean.getAreaCode());
        CityArea cityArea=cityAreaMapper.selectOne(queryWrapper);

        CommunityInfo communityInfo = new CommunityInfo();
        BeanUtils.copyBeanProp(communityInfo,communityInfoBean);

        communityInfo.setAreaCode(cityArea.getId());
        int row = 0;
        if(communityInfo.getId() != null){
            row=communityInfoMapper.updateById(communityInfo);
        }else{
//            添加
            row =communityInfoMapper.insert(communityInfo);
        }
        if(row<=0){
            return Result.failure(ResultCode.PARAM_IS_INVALID);
        }
        return Result.success();
    }

}

